import sys
import detect_faces as df

def main():
    mFile = open("Twitter_user_county_2014_traj.txt","rb")
    mOut = open("Twitter_user_county_2014_face.txt","wb")
    for mRow in mFile:
        mEle = mRow.rstrip().split(',')
        # print mEle[3]
        ###prepare a Twitter user's screen name
        ### screen_name = mEle[3] 
        ww = df.getUser(mEle[3])
        ### Face API retuns values
        if ww != []:
            result =[]
            for mCell in ww:
                if mCell != {}:
                    if 'age' in mCell and 'gender' in mCell:
                        result.append(str(mCell['age'])+ '&' + mCell['gender'])
            mString = ":".join(result)
            mOut.write(mRow.replace('\n','') + ',' + str(mString) + '\n')
        ###If Face API does not provide valid values
        else:
            mOut.write(mRow.rstrip() + ',' + '-1&-1' + '\n')
    mOut.close()

if __name__=='__main__':
    print "programming started"
    main()
